package com.hhit.zhgd.controller

import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper
import com.baomidou.mybatisplus.core.metadata.IPage
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.hhit.zhgd.component.FileProperties
import com.hhit.zhgd.domain.dto.BaskStageHighlightGreenConstructionDTO
import com.hhit.zhgd.domain.entity.ZhgdHighlightGreenConstruction
import com.hhit.zhgd.domain.response.BaseResp
import com.hhit.zhgd.service.ZhgdHighlightGreenConstructionService
import jakarta.annotation.Resource
import org.apache.commons.lang3.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.validation.BindingResult
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import java.io.File
import javax.validation.Valid

@RestController
class ZhgdHighlightGreenConstructionController {

    @Autowired
    private val zhgdHighlightGreenConstructionService: ZhgdHighlightGreenConstructionService? = null

    @Resource
    private val fileProperties: FileProperties? = null

    /**
     * @描述 导入 亮点、绿色建造 数据
     * @参数 [multipartFile]
     * @返回值 com.hhit.zhgd.utils.R
     * @创建人 qianchengcheng
     * @创建时间 2023/5/19
     * @修改人和其它信息
     */
    @PostMapping("/importHighlightGreenConstruction")
    @Throws(Exception::class)
    fun importHighlightGreenConstruction(@RequestParam("file") multipartFile: MultipartFile?): BaseResp<Any?>? {
        return zhgdHighlightGreenConstructionService?.importHighlightGreenConstruction(multipartFile)
    }

    /**
     * @描述 分页查询 亮点、绿色建造数据
     * @参数 [page, size]
     * @返回值 com.hhit.zhgd.utils.R
     * @创建人 qianchengcheng
     * @创建时间 2023/5/19
     * @修改人和其它信息
     */
    @GetMapping("/getHighlightGreenConstructions")
    fun getHighlightGreenConstructions(
        @RequestParam(name = "page", defaultValue = "1") page: Int,
        @RequestParam(name = "size", defaultValue = "15") size: Int,
        @RequestParam(name = "affiliatedUnit", required = false) affiliatedUnit: String?
    ): BaseResp<Any?>? {
        val pageInfo = Page<ZhgdHighlightGreenConstruction>(page.toLong(), size.toLong())
        val query = UpdateWrapper<ZhgdHighlightGreenConstruction>()
        if (StrUtil.isNotBlank(affiliatedUnit)) {
            query.eq("affiliated_unit", affiliatedUnit)
        }
        val page1: IPage<ZhgdHighlightGreenConstruction>? = zhgdHighlightGreenConstructionService?.page(pageInfo, query)
        return BaseResp.success(page1)
    }

    /**
     * @描述 质量管理-亮点落实情况
     * @参数 []
     * @返回值 com.hhit.zhgd.utils.R
     * @创建人 qianchengcheng
     * @创建时间 2023/5/23
     * @修改人和其它信息
     */
    @GetMapping("/getHighlightSituation")
    fun getHighlightSituation(): BaseResp<Any?>? {
        return BaseResp.success(zhgdHighlightGreenConstructionService?.getHighlightSituation())
    }

    /**
     * @描述 质量管理-绿色建造得分率
     * @参数 []
     * @返回值 com.hhit.zhgd.utils.R
     * @创建人 qianchengcheng
     * @创建时间 2023/5/23
     * @修改人和其它信息
     */
    @GetMapping("/getGreenConstructionScoreRate")
    fun getGreenConstructionScoreRate(): BaseResp<Any?>? {
        return BaseResp.success(zhgdHighlightGreenConstructionService?.getGreenConstructionScoreRate())
    }

    @PostMapping("/addHighlightGreenConstruction")
    fun addHighlightGreenConstruction(@RequestBody highlightGreenConstruction: ZhgdHighlightGreenConstruction): BaseResp<Any?>? {
        zhgdHighlightGreenConstructionService!!.save(highlightGreenConstruction)
        return BaseResp.success()
    }


    @PostMapping("/editHighlightGreenConstruction")
    fun editHighlightGreenConstruction(@RequestBody highlightGreenConstruction: ZhgdHighlightGreenConstruction): BaseResp<Any?>? {
        zhgdHighlightGreenConstructionService?.updateById(highlightGreenConstruction)
        return BaseResp.success()
    }


    @GetMapping("/deleteById")
    fun deleteById(@RequestParam("idList") idList: List<Int?>): BaseResp<Any?>? {
        for (id in idList) {
            val entity = zhgdHighlightGreenConstructionService?.getById(id)
            if (StringUtils.isNotBlank(entity?.fileUrls)) {
                for (fileUrl in entity?.fileUrls?.split(",".toRegex())?.dropLastWhile { it.isEmpty() }?.toTypedArray()?: emptyArray()) {
                    val path: String = fileProperties?.parentPath + fileUrl
                    File(path).delete()
                }
            }
            zhgdHighlightGreenConstructionService?.removeById(id)
        }
        return BaseResp.success()
    }

    /**
     * @description 后台管理-质量管理-新增亮点落实
     * @author qianchengcheng
     * @date 2024/7/16 14:23
     */
    @PostMapping("backStage/highlightGreenConstructionAdd")
    fun highlightGreenConstructionAdd(
        baskStageHighlightGreenConstructionDTO: @Valid BaskStageHighlightGreenConstructionDTO?,
        bindingResult: BindingResult
    ): BaseResp<Any?>? {
        return try {
            if (bindingResult.hasErrors()) {
                val defaultMessage = bindingResult.fieldError!!.defaultMessage
                return BaseResp.fail(defaultMessage)
            }
            zhgdHighlightGreenConstructionService!!.highlightGreenConstructionAdd(baskStageHighlightGreenConstructionDTO)
        } catch (e: Exception) {
            BaseResp.fail("添加异常!" + e.message)
        }
    }

    /**
     * @description 后台管理-质量管理-修改亮点落实
     * @author qianchengcheng
     * @date 2024/7/16 15:55
     */
    @PostMapping("backStage/highlightGreenConstructionEdit")
    fun highlightGreenConstructionEdit(
        baskStageHighlightGreenConstructionDTO: @Valid BaskStageHighlightGreenConstructionDTO?,
        bindingResult: BindingResult
    ): BaseResp<Any?>? {
        return try {
            if (bindingResult.hasErrors()) {
                val defaultMessage = bindingResult.fieldError!!.defaultMessage
                return BaseResp.fail(defaultMessage)
            }
            zhgdHighlightGreenConstructionService!!.highlightGreenConstructionEdit(
                baskStageHighlightGreenConstructionDTO
            )
        } catch (e: Exception) {
            BaseResp.fail("修改!" + e.message)
        }
    }

    /**
     * @description 后台管理-质量管理-分页查询亮点落实
     * @author qianchengcheng
     * @date 2024/7/16 16:16
     */
    @GetMapping("backStage/getHighlightGreenConstructionPageInfo")
    fun getHighlightGreenConstructionPageInfo(
        @RequestParam("keyWord") keyWord: String?,
        @RequestParam(value = "page", defaultValue = "1") page: Int?,
        @RequestParam(value = "size", defaultValue = "15") size: Int?
    ): BaseResp<Any?>? {
        return BaseResp.success(
            zhgdHighlightGreenConstructionService!!.getHighlightGreenConstructionPageInfo(
                keyWord,
                page!!, size!!
            )
        )
    }

    /**
     * @description 后台管理-质量管理-删除亮点落实
     * @author qianchengcheng
     * @date 2024/7/16 17:08
     */
    @GetMapping("backStage/highlightGreenConstructionDel")
    fun highlightGreenConstructionDel(@RequestParam("id") id: Int?): BaseResp<Any?>? {
        return zhgdHighlightGreenConstructionService?.highlightGreenConstructionDel(id)
    }

}